<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\News\NewsClass;
use App\Models\Template;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Form as BForm;
use Encore\Admin\Widgets\Table;
use Illuminate\Validation\Rule;
class ColumnController extends Controller
{
    protected $name = '分类管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body(NewsClass::tree(function ($tree) {
                $tree->tools(function($tool){
                    $tool->add('<a class="btn btn-info btn-sm" onclick="build_common(\'NewsClass\', 0)">生成所有</a>');
                    $tool->add('<a class="btn btn-info btn-sm" href="/admin/column/play_class_build">计划生成</a>');
                });

            }));
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form($id)->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    //后台计划任务生成列表
    public function play_class_build()
    {
        if(request()->isMethod('post'))
        {
            // dd(request()->get('created_at'));
            $data = [
                'class_id' => request()->get('class_id'),
                'status' => 0
            ];
            cache(['play_class_build'=>$data], 30000);
            admin_toastr('创建成功，请耐心等候...', 'success');
            return redirect('admin/column');
        }
        return Admin::content(function (Content $content) {

            $content->header('计划更新列表页');
            $content->description('先创建计划，1个小时后执行');
            $headers = ['栏目', '状态'];
            $data = cache('play_class_build');
            $rows = [];
            if($data)
            {
                $variable = NewsClass::whereIn('id',$data['class_id'])->get();
                $class_name = '';
                foreach ($variable as $key => $value) {
                    # code...
                    $class_name .= $value->name.'|';
                }
                if($class_name == '')$class_name = '全部';
                $status = '';
                if(isset($data['status']))
                {
                    if($data['status']==0)
                    {
                        $status = '待执行';
                    }elseif($data['status']==1){
                        $status = '正在执行';
                    }elseif($data['status']==2){
                        $status = '执行完成';
                    }
                }
                $rows = [
                    [$class_name, $status]
                ];
            }
            

            $table = new Table($headers, $rows);

            $form = new BForm();

            $form->multipleSelect('class_id', '栏目')->options(NewsClass::selectOptions())->help('root为全部');
            $form->disablePjax();

            $content->body($table->render().$form->render());
            
            
        });
        
    }

    public function update($id)
    {
        return $this->form($id)->update($id);
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = null)
    {
        return Admin::form(NewsClass::class, function (Form $form) use ($id) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('name', '分类名称')->rules('required');
            $form->text('alias', '别名')->rules('required|max:30|unique:news_classes,alias,'.$id)->help('英文名称，用于生成静态目录');
            $form->number('listorder', '排序');
            $form->number('limit_num', '每页显示数量');
            $form->select('parent_id', '父栏目')->options(NewsClass::selectOptions())->rules('required');
            $form->radio('is_page','单页面')->options(['0'=>'否','1'=> '是'])->help('单页面：只生成封面页');

            $form->select('index_temp_id', '封面模板')->options(Template::selectOptions(2));
            $form->select('cont_temp_id', '内容模板')->options(Template::selectOptions(3));
            
            
            $form->text('seo_title', 'SEO标题');
            $form->text('seo_keywords', 'SEO关键词');
            $form->text('seo_description', 'SEO描述');
            // $form->editor('contentccc', '描述');//->rules('required')
            // $form->editor('content', '内容')->help('如果是单页内容则填写，比如关于我们，联系我们');
            $form->tools(function (Form\Tools $tools) {
                // 去掉`查看`checkbox
                $tools->disableView();
            });
            
            
        });
    }
}
